package net.zjitc.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import net.zjitc.domain.User;
import net.zjitc.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;


/**
 * Created by Administrator on 2017/2/27.
 */
@Controller
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User>{

    //定义一个log变量
    private Log log = LogFactory.getLog(this.getClass());

    private User Model = new User();

    public User getModel() {
        return Model;
    }



    @Resource
    private UserService userService;
    /*登录界面*/
    public String loginUI(){
        return "loginUI";
    }
    /*登录请求*/
    public String login(){
        //如果有用户返回登录成功
        User user=userService.findByUsernamePassword(Model.getUsername(),Model.getPassworld());
        System.out.println(Model.getUsername());
        System.out.println(Model.getPassworld());
//        ActionContext.getContext().getValueStack().push(user);
        ActionContext.getContext().getSession().put("user",user);
        if (user!=null){
            log.debug("登录成功");
            List<User>userList=userService.findAll();
            this.addActionMessage("登录成功");
            return "loginSuccess";
        }
        //否则返回登录失败
        this.addActionError("用户名或密码错误");
        return "loginFail";
    }


    //注册页面
    public String registrUI(){
        return "registrUI";
    }
    //注册请求
    public String registr(){

        Boolean registr=userService.findByRegistr(Model.getUsername(),Model.getPassworld());
            if (registr){
                this.addActionMessage("注册成功");
                return "registrSuccess";
            }else {
                //否则返回注册失败
                this.addActionError("用户名重复");
                return "registrFail";
            }
    };
}
